package com.jd.risk.bankorder.controller.bankorder;

import com.alibaba.fastjson.JSON;
import com.jd.risk.bankorder.common.CommonResult;
import com.jd.risk.bankorder.controller.BaseController;
import com.jd.risk.bankorder.entity.bankorder.TeslaBankRequest;
import com.jd.risk.bankorder.entity.bankorder.TeslaOrderInfo;
import com.jd.risk.bankorder.entity.bankorder.TeslaReplyInfo;
import com.jd.risk.bankorder.service.bankorder.BankOrderReplyService;
import com.jd.risk.bankorder.service.bankorder.BankOrderService;
import com.jd.risk.bankorder.util.CommonUtil;
import com.wycds.datacenter.api.DataReadService;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by songjunou on 2017/5/19.
 */
@Controller
@RequestMapping("/bank/bankOrderReply")
public class BankOrderReplyController extends BaseController {

    @Autowired
    private BankOrderService bankOrderService;

    @Autowired
    private BankOrderReplyService bankOrderReplyService;


    @RequestMapping("/toBankOrderListReplyPage")
    public String toBankOrderListReply(ModelMap map)
    {
        map.put("bankRequestType","0");
        return "bankorder/bankOrderReplyList";
    }



    @ResponseBody
    @RequestMapping("/bankOrderListReply")
    public String bankOrderListReply(TeslaBankRequest request){

        String bankRequestStateRange = "'1','2'";
        request.setBankRequestStateRange(bankRequestStateRange);
        List<TeslaBankRequest> reqList = bankOrderService.queryBankRequest(request);
        Long count = bankOrderService.queryBankRequestCont(request);

        CommonResult<TeslaBankRequest> result = new CommonResult<TeslaBankRequest>();
        result.setSuccess(true);
        result.setRows(reqList);
        result.setTotal(count);
        return toJson(result);
    }

    /**
     *  详情页面
     * @param map
     * @param requestId
     * @return
     */
    @RequestMapping("toBankOrderDetailReplyPage")
    public String toBankOrderDetailRequestPage(ModelMap map , Long requestId){
        bankOrderReplyService.getAllDetailForShow(map,requestId);
        return "bankorder/bankOrderDetailReplyPage";
    }

    @RequestMapping("toBankOrderDetailReplyModifyPage")
    public String toBankOrderDetailRequestModifyPage(ModelMap map ,  Long requestId){
        bankOrderReplyService.getAllDetailForShow(map,requestId);
        return "bankorder/bankOrderDetailReplyModifyPage";
    }

    @ResponseBody
    @RequestMapping("/saveOrSendReply")
    public String saveOrSendReply(@RequestParam( value = "orderstr" ,required = true) String orderstr ,
                                  @RequestParam(value = "replystr",required = true) String replystr,
                                  @RequestParam(value = "requeststr",required = false) String requeststr
                                  )
    {
        List<TeslaOrderInfo> orderInfo = JSON.parseArray(orderstr , TeslaOrderInfo.class);
        TeslaReplyInfo replyInfo = JSON.parseObject(replystr , TeslaReplyInfo.class);
        TeslaBankRequest bankRequest =null;
        if(StringUtils.isNotBlank(requeststr))
        {
             bankRequest = JSON.parseObject(requeststr , TeslaBankRequest.class);
        }
        bankOrderReplyService.saveOrSendReply(orderInfo ,replyInfo ,bankRequest);
        Map<String,Object> map = new HashMap<String, Object>();
        map.put("success",true);
        return toJson(map);
    }

    @ResponseBody
    @RequestMapping("/uploadFile")
    public  String uploadFile(@RequestParam MultipartFile uploadObject)
    {
      return  bankOrderService.uploadFile(uploadObject);
    }

    @ResponseBody
    @RequestMapping("/deleteFile")
    public  String deleteFile(String fileName)
    {
      return  bankOrderService.deleteFile(fileName);
    }

    @RequestMapping("/downloadFile")
    public ResponseEntity<byte[]> downloadFile(String fileName)
    {
        return bankOrderService.downloadFile(fileName);
    }


    @RequestMapping("/bankOrderExport")
    public ResponseEntity<byte[]> bankOrderExport(TeslaBankRequest request)
    {
        String bankRequestStateRange = "'1','2'";
        request.setBankRequestStateRange(bankRequestStateRange);
        String fileName ="";
        if(request.getBankRequestType().equals("0")){
            fileName = "银行订单号管理导出-"+ CommonUtil.getFormatCommonSSS().format(new Date())+".csv";
        }else{
            fileName = "银行卡号管理导出-"+ CommonUtil.getFormatCommonSSS().format(new Date())+".csv";
        }
        HttpHeaders headers = new HttpHeaders();
        String content  = bankOrderService.exportReply(request);
        headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
        try {
            headers.setContentDispositionFormData("attachment", new String(fileName.getBytes("utf-8"), "ISO8859-1"));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        HttpStatus statusCode = HttpStatus.OK;
        ResponseEntity<byte[]> entity = new ResponseEntity<byte[]>(content.getBytes(), headers, statusCode);
        return entity;
    }
}
